package com.yhq.spring.spring.conf;

import com.yhq.spring.spring.po.User;
import org.springframework.util.StringUtils;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;

/**
 * @author hqyin
 * @date 2023/4/20 4:57 下午
 */
public class StringToUserPropertyEditor extends PropertyEditorSupport implements PropertyEditor {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if(StringUtils.hasText(text)) {
            User user = new User();
//            user.setName(text);
//            user.setAge(11);
            this.setValue(user);
        } else {
            this.setValue(null);
        }
    }

    @Override
    public String getAsText() {
        User user = (User) this.getValue();
        if(user != null) {
            return user.toString();
        } else {
            return "";
        }
    }
}
